package cn.stay.dd.impl;

import cn.stay.dd.IUserDao;
import cn.stay.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author Lorry
 * @since 2019-10-26 21:44
 **/
@Repository
public class UserDaoImp implements IUserDao {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean add(User user) {
        //使用最基本的方式
        /*redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(redisTemplate.getStringSerializer().serialize(user.getUsername()),
                        redisTemplate.getStringSerializer().serialize(user.getPassword()));
                return null;
            }
        });*/
        //利用了StringRedisTemplate的特性
        redisTemplate.opsForValue().set(user.getUsername(), user.getPassword());

        //利用了StringRedisTemplate的特性 通过绑定的方式
//        BoundValueOperations<String, String> bound = redisTemplate.boundValueOps(user.getUsername());
//        bound.set(user.getPassword());
        //bound.append(user.getPassword());//追加，和StringBuilder的append一样功能
        return true;
    }

    @Override
    public boolean delete(String key) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }
}
